/*
 * @Author: gggg 1263457581@qq.com
 * @Date: 2025-06-03 16:21:46
 * @LastEditors: gggg 1263457581@qq.com
 * @LastEditTime: 2025-06-30 09:14:06
 * @FilePath: /algorithm-and-data-struct/c++11/MyAutoPtr.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/**
 * @file MyAutoPtr.cpp
 * 原理  将指针包装在类中 利用类对象的自动析构释放指针指向的对象
 **/
#include <iostream>

template<class T>
class MyAutoPtr {
public:
    MyAutoPtr(T *p = nullptr) : m_p(p) {}
    ~MyAutoPtr() { delete m_p; }
    T &operator*() { return *m_p; }
    T *operator->() { return m_p; }
    MyAutoPtr(MyAutoPtr &&other) : m_p(other.m_p) { other.m_p = nullptr; }
    MyAutoPtr &operator=(MyAutoPtr &&other) {
        if (this != &other) {
            delete m_p;
            m_p = other.m_p;
            other.m_p = nullptr;
        }
        return *this;
    }
private:
    T *m_p;
};

